﻿

namespace Mulala.Cavan.Presentation.Mvc.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Application.Seedwork;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Core.Helper;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Presentation.Mvc.Controller;

    public class CorporationViewModelConverter
    {
        public static void Convert(Corporation corporation, ICorporationViewModel corporationViewModel)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;

            corporationViewModel.Type = corporation.Type.GetHashCode().ToString().ConvertToTagText(DictionaryIdentifier.GroupType.CorporationType);
            corporationViewModel.Nature = corporation.Nature.ConvertToTagText();
            corporationViewModel.BusinessExperience = corporation.BusinessExperience.ConvertToTagText();
            corporationViewModel.Scale = corporation.Scale.ConvertToTagText();
            corporationViewModel.Speciality = corporation.Speciality.ToPositionArray().ConvertToTagText(DictionaryIdentifier.GroupType.CorporationSpeciality);
            corporationViewModel.Logo = attachmentAppService.GetCorporationLogo(corporation);
            corporationViewModel.UserAccountIcon = GetCorporationUserAccountIcon(corporation.UserAccount.Score);
            corporationViewModel.ExperienceIcon = GetCorporationExperienceIcon(corporation.UserAccount.Experience);
            corporationViewModel.DetailUrl = Project.Settings.HostServer + "/" + ControllerConst.CORPORATION + "/" + ActionConst.DETAIL + "/" + corporation.ID.ToString();
            corporationViewModel.Item = corporation;
        }

        public static void ConvertForEdit(ICorporationViewModel corporationViewModel, Corporation corporation)
        {
            corporationViewModel.Specialities = corporation.Speciality.ToPosition();
            corporationViewModel.TypeForEdit = corporation.Type.GetHashCode();
            corporationViewModel.CityID = corporation.Area.ID.Substring(0, 4);
            corporationViewModel.TownID = corporation.Area.ID;
            corporationViewModel.Item = corporation;
            foreach (string s in corporationViewModel.Specialities)
            {
                corporationViewModel.Speciality = corporationViewModel.Speciality + s + ",";
            }
        }

        public static void ConvertForEdit(Corporation corporation, ICorporationViewModel corporationViewModel)
        {
            corporation.Speciality = StringHelper.ClearLastChar(corporationViewModel.Speciality).ToBinaryValue();
            corporation.Type = (CorporationIdentifier.CorporationType)corporationViewModel.TypeForEdit;
        }

        public static ICorporationsViewModel Convert(IList<Corporation> corporations, bool isConvertAll)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;

            ICorporationsViewModel corporationsViewModel = ModelFactory.CreateModel<ICorporationsViewModel>();

            IList<ICorporationViewModel> corporationViewModels = new List<ICorporationViewModel>();
            if (null != corporations)
            {
                foreach (Corporation corporation in corporations)
                {
                    ICorporationViewModel corporationViewModel = ModelFactory.CreateModel<ICorporationViewModel>();
                    if (isConvertAll)
                    {
                        CorporationViewModelConverter.Convert(corporation, corporationViewModel);
                    }
                    else
                    {
                        corporationViewModel.Type = corporation.Type.GetHashCode().ToString().ConvertToTagText(DictionaryIdentifier.GroupType.CorporationType);
                        corporationViewModel.DetailUrl = Project.Settings.HostServer + "/" + ControllerConst.CORPORATION + "/" + ActionConst.DETAIL + "/" + corporation.ID.ToString();
                    }
                    corporationViewModel.Item = corporation;
                    corporationViewModel.Logo = attachmentAppService.GetCorporationLogo(corporation);

                    corporationViewModels.Add(corporationViewModel);
                }
                corporationsViewModel.Corporations = corporationViewModels;
            }

            return corporationsViewModel;
        }

        private static string GetCorporationUserAccountIcon(int score)
        {
            if (score <= Project.Settings.NormalUserAccountMaxScore)
            {
                return string.Empty;
            }
            else if (score < Project.Settings.BronzeUserAccountMaxScore)
            {
                return Project.Settings.BronzeUserAccountIcon;
            }
            else if (score < Project.Settings.SilverUserAccountMaxScore)
            {
                return Project.Settings.SilverUserAccountIcon;
            }
            else if (score < Project.Settings.GoldUserAccountMaxScore)
            {
                return Project.Settings.GoldUserAccountIcon;
            }
            else if (score < Project.Settings.DiamondUserAccountMaxScore)
            {
                return Project.Settings.DiamondUserAccountIcon;
            }
            return string.Empty;
        }

        private static string GetCorporationExperienceIcon(int experience)
        {
            if (experience <= Project.Settings.CorporationFiveLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "1";
            }
            else if (experience < Project.Settings.CorporationTwoLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "2";
            }
            else if (experience < Project.Settings.CorporationTreeLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "3";
            }
            else if (experience < Project.Settings.CorporationFourLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "4";
            }
            else if (experience < Project.Settings.CorporationFiveLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "5";
            }
            return string.Empty;
        }
    }
}
